package com.atguigu.repservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.repservice.entity.RepOrder;
import com.atguigu.repservice.entity.vo.OrderRequestVo;
import com.atguigu.repservice.entity.vo.RepOrderRequestVo;
import com.atguigu.repservice.service.RepOrderService;
import com.atguigu.repservice.service.RepSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZhuangLiJiao
 * @since 2021-10-23
 */
@Api(description = "订单接口")
@RestController
@RequestMapping("/repservice/reporder")
public class AdminRepOrderController {

    @Autowired
    private RepOrderService service;

    @Autowired
    private RepSubjectService subjectService;

    //2.条件查询订单带分页的方法
    @ApiOperation("分页条件查询订单")
    @PostMapping("pageOrderCondition/{current}/{limit}")
    public R pageOrderCondition(@PathVariable("current") long current, @PathVariable("limit") long limit,
                                @RequestBody(required = false) OrderRequestVo.OrderQueryVo orderRequestVo) {
        //创建page对象
        Page<RepOrder> orderPage = service.pageOrderCondition(current, limit, orderRequestVo);
//        System.out.println("大小getSize："+orderPage.getSize());
//        System.out.println("大小Pages："+orderPage.getPages());
//        System.out.println("大小total："+orderPage.getTotal());.
        long total = orderPage.getTotal();//总记录数
        List<RepOrder> records = orderPage.getRecords(); //数据list集合
        return R.ok().data("total", total).data("rows", records);
    }


    //2.根据id查询订单
    @ApiOperation("根据id查询订单")
    @GetMapping("getOrderById/{id}")
    public R getOrderById(@PathVariable("id") String id) {
        RepOrder o = service.getById(id);
        o.setSubjectId(subjectService.getById(o.getSubjectId()).getTitle());
        o.setSubjectParentId(subjectService.getById(o.getSubjectParentId()).getTitle());
        return R.ok().data("getOrderById", o);
    }


    //3.修改订单状态
    @ApiOperation("修改订单状态")
    @GetMapping("modifyOrderStatus/{orderId}/{number}")
    public R modifyOrderStatus(@PathVariable("orderId") String orderId, @PathVariable("number") String number) {
        RepOrder order = service.getById(orderId);
        order.setStatus(number);
        service.updateById(order);
        return R.ok();
    }
}

